using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;

public class HumanCharacter : CharacterSystem
{
	[SyncVar]
	public byte Pill;

	public byte PillMax = 100;

	public byte RegetRate = 1;

	public bool SpectreOnDead = true;

	private static int kCmdCmdUsepill;

	private static int kCmdCmdUseheal;

	public byte NetworkPill
	{
		get
		{
			return Pill;
		}
		[param: In]
		set
		{
			SetSyncVar(value, ref Pill, 16u);
		}
	}

	public override void Start()
	{
		if ((bool)animator)
		{
			animator.SetInteger("Shoot_Type", AttackType);
		}
		InvokeRepeating("pillUpdate", 1f, 1f);
		base.Start();
	}

	public override void DoApplyDamage(byte damage, Vector3 direction, int attackerID, byte team)
	{
		if (!UnitZ.gameManager || UnitZ.gameManager.IsBattleStart)
		{
			base.DoApplyDamage(damage, direction, attackerID, team);
		}
	}

	public override void Update()
	{
		if (!(animator == null))
		{
			animator.SetInteger("BodyState", MovementIndex);
			base.Update();
		}
	}

	public override void PlayMoveAnimation(float magnitude, float side)
	{
		if ((bool)animator)
		{
			animator.SetFloat("Velocity", magnitude);
			animator.SetFloat("SideVelocity", side);
		}
		base.PlayMoveAnimation(magnitude, side);
	}

	public override void PlayAttackAnimation(bool attacking, int attacktype)
	{
		if ((bool)animator)
		{
			if (attacking)
			{
				animator.SetTrigger("Shoot");
			}
			animator.SetInteger("Shoot_Type", attacktype);
		}
		base.PlayAttackAnimation(attacking, attacktype);
	}

	public void OnEquipChanged(int type)
	{
		if ((bool)animator)
		{
			animator.SetInteger("Shoot_Type", type);
		}
	}

	public override void OnDeadBodySpawned(GameObject deadbody)
	{
		SpectreCamera spectreCamera = (SpectreCamera)Object.FindObjectOfType(typeof(SpectreCamera));
		if ((bool)spectreCamera && SpectreOnDead && IsMine)
		{
			spectreCamera.LookingAtObject(deadbody);
		}
		base.OnDeadBodySpawned(deadbody);
	}

	public override void SetEnable(bool enable)
	{
		if ((bool)GetComponent<PlayerView>())
		{
			GetComponent<PlayerView>().enabled = enable;
		}
		if ((bool)GetComponent<FPSController>())
		{
			GetComponent<FPSController>().enabled = enable;
		}
		if ((bool)GetComponent<CharacterMotor>())
		{
			GetComponent<CharacterMotor>().enabled = enable;
			GetComponent<CharacterMotor>().Reset();
		}
		if ((bool)GetComponent<CharacterController>())
		{
			GetComponent<CharacterController>().enabled = enable;
		}
		if ((bool)GetComponent<NetworkTransform>())
		{
			GetComponent<NetworkTransform>().enabled = enable;
		}
		if ((bool)GetComponent<CharacterDriver>())
		{
			GetComponent<CharacterDriver>().NoVehicle();
		}
		base.SetEnable(enable);
	}

	public override void OnKilled(int killer, int me, string killtype)
	{
		if ((bool)UnitZ.NetworkGameplay.scoreManager)
		{
			if (NetID != -1)
			{
				UnitZ.NetworkGameplay.PlayerKilled(base.connectionToClient);
			}
			UnitZ.NetworkGameplay.scoreManager.UpdateScore(LastHitByID, NetID, 1);
		}
		base.OnKilled(killer, me, killtype);
	}

	public override void OnThisThingDead()
	{
		if (NetID != -1)
		{
			RemoveCharacterData();
		}
		CharacterItemDroper component = GetComponent<CharacterItemDroper>();
		if ((bool)component)
		{
			component.DropItem();
		}
		if (base.isServer)
		{
			ItemDropAfterDead component2 = GetComponent<ItemDropAfterDead>();
			if ((bool)component2)
			{
				component2.DropItem();
			}
		}
		base.OnThisThingDead();
	}

	public override void OnRespawn(int spawner)
	{
		if (base.isLocalPlayer)
		{
			base.transform.position = UnitZ.playerManager.FindASpawnPoint(spawner);
		}
		SendMessage("Respawn", SendMessageOptions.DontRequireReceiver);
		if ((bool)GetComponent<CharacterInventory>())
		{
			GetComponent<CharacterInventory>().SetupStarterItem();
		}
		base.OnRespawn(spawner);
	}

	public override void OnDestroyed()
	{
		if (base.isServer && NetID != -1 && UnitZ.NetworkGameplay.playersManager != null)
		{
			UnitZ.NetworkGameplay.playersManager.RemovePlayer(NetID);
		}
		base.OnDestroyed();
	}

	[Command]
	private void CmdUsepill(byte num)
	{
		if (Pill + num > PillMax)
		{
			NetworkPill = PillMax;
		}
		else
		{
			NetworkPill = (byte)(Pill + num);
		}
	}

	public void UsePill(byte num)
	{
		CallCmdUsepill(num);
	}

	[Command]
	private void CmdUseheal(byte num)
	{
		if (HP + num > HPmax)
		{
			base.NetworkHP = HPmax;
		}
		base.NetworkHP = (byte)(HP + num);
	}

	public void UseHeal(byte num)
	{
		CallCmdUseheal(num);
	}

	private void pillUpdate()
	{
		if (!base.isServer || Pill <= 0)
		{
			return;
		}
		if (HP < HPmax)
		{
			if (HP + RegetRate > HPmax)
			{
				base.NetworkHP = HPmax;
			}
			else
			{
				base.NetworkHP = (byte)(HP + RegetRate);
			}
		}
		NetworkPill = (byte)(Pill - 1);
	}

	private void UNetVersion()
	{
	}

	protected static void InvokeCmdCmdUsepill(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdUsepill called on client.");
		}
		else
		{
			((HumanCharacter)obj).CmdUsepill((byte)reader.ReadPackedUInt32());
		}
	}

	protected static void InvokeCmdCmdUseheal(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdUseheal called on client.");
		}
		else
		{
			((HumanCharacter)obj).CmdUseheal((byte)reader.ReadPackedUInt32());
		}
	}

	public void CallCmdUsepill(byte num)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdUsepill called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdUsepill(num);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdUsepill);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.WritePackedUInt32(num);
		SendCommandInternal(networkWriter, 0, "CmdUsepill");
	}

	public void CallCmdUseheal(byte num)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdUseheal called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdUseheal(num);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdUseheal);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.WritePackedUInt32(num);
		SendCommandInternal(networkWriter, 0, "CmdUseheal");
	}

	static HumanCharacter()
	{
		kCmdCmdUsepill = -1429943868;
		NetworkBehaviour.RegisterCommandDelegate(typeof(HumanCharacter), kCmdCmdUsepill, InvokeCmdCmdUsepill);
		kCmdCmdUseheal = -1430186381;
		NetworkBehaviour.RegisterCommandDelegate(typeof(HumanCharacter), kCmdCmdUseheal, InvokeCmdCmdUseheal);
		NetworkCRC.RegisterBehaviour("HumanCharacter", 0);
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		bool flag = base.OnSerialize(writer, forceAll);
		if (forceAll)
		{
			writer.WritePackedUInt32(Pill);
			return true;
		}
		bool flag2 = false;
		if ((base.syncVarDirtyBits & 0x10) != 0)
		{
			if (!flag2)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag2 = true;
			}
			writer.WritePackedUInt32(Pill);
		}
		if (!flag2)
		{
			writer.WritePackedUInt32(base.syncVarDirtyBits);
		}
		return flag2 | flag;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
		base.OnDeserialize(reader, initialState);
		if (initialState)
		{
			Pill = (byte)reader.ReadPackedUInt32();
			return;
		}
		int num = (int)reader.ReadPackedUInt32();
		if ((num & 0x10) != 0)
		{
			Pill = (byte)reader.ReadPackedUInt32();
		}
	}
}
